package com.zhudi.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.zhudi.domain.Expert;
import com.zhudi.domain.Graduation;
import com.zhudi.math.Objective;
import com.zhudi.math.Subjective;
import com.zhudi.math.TopSis;
import com.zhudi.service.IGraduationService;
import com.zhudi.utils.ExcelUtils;
import com.zhudi.utils.ZhuDiUtil;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author chenjc
 * @since 2021/02/25 10:24
 */
@Service
public class GraduationServiceImpl implements IGraduationService {

    @Override
    public Graduation calWeight(InputStream expert, InputStream original) {
        //读取原始指标数据
        BigDecimal[][] originalData = ExcelUtils.readExcelData(original);
        //读取专家数据
        List<Expert> experts = ExcelUtils.readExpertData(expert);
        //转换为标准化矩阵
        BigDecimal[][] standard = Objective.tranStandard(originalData);
        //计算主观权重和专家贡献度
        Graduation graduation = Subjective.getSubject(experts);
        //计算客观权重向量
        BigDecimal[] object = Objective.getObject(ZhuDiUtil.standardX);
        //计算综合权重
        BigDecimal[] comprehensiveData = Objective.getComprehensive(graduation.getSubjectiveData(), object);
        //计算topSis
        BigDecimal[] topSisData = TopSis.getTopSis(standard, comprehensiveData);

        graduation.setObjectiveData(object);
        graduation.setTopSisData(topSisData);
        graduation.setComprehensiveData(comprehensiveData);
        return graduation;
    }

    @Override
    public AjaxResult createResultFile(Graduation graduation) {
        if (null == graduation) {
            return AjaxResult.error(ZhuDiUtil.GRADUATION_EMPTY);
        }
        //取出结果集
        Map<String, BigDecimal> expertContribution = graduation.getExpertContribution();
        BigDecimal[] subjectiveData = graduation.getSubjectiveData();
        BigDecimal[] objectiveData = graduation.getObjectiveData();
        BigDecimal[] comprehensiveData = graduation.getComprehensiveData();
        BigDecimal[] topSisData = graduation.getTopSisData();
        //创建工作簿
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //创建工作表
        XSSFSheet xssfSheet = xssfWorkbook.createSheet();
        //创建结果格式
        xssfSheet.createRow(1).createCell(0).setCellValue(ZhuDiUtil.EXPERT_TITLE);
        xssfSheet.createRow(2);
        xssfSheet.addMergedRegion(new CellRangeAddress(1, 2, 0, 0));
        xssfSheet.createRow(5).createCell(0).setCellValue(ZhuDiUtil.SUBJECTIVE_TITLE);
        xssfSheet.createRow(6).createCell(0).setCellValue(ZhuDiUtil.OBJECTIVE_TITLE);
        xssfSheet.createRow(7).createCell(0).setCellValue(ZhuDiUtil.COMPREHENSIVE_TITLE);
        xssfSheet.createRow(9).createCell(0).setCellValue(ZhuDiUtil.TOP_SIS_TITLE);
        xssfSheet.addMergedRegion(new CellRangeAddress(9, 10, 0, 0));
        //写入专家贡献度
        if (null != expertContribution) {
            Set<String> expertNames = expertContribution.keySet();
            String contribution = null;
            int i = 1;
            for (String expertName : expertNames) {
                contribution = expertContribution.get(expertName).toString();
                xssfSheet.getRow(1).createCell(i).setCellValue(expertName);
                xssfSheet.getRow(2).createCell(i).setCellValue(contribution);
                i++;
            }
        }
        //写入指标名
        if (null != ZhuDiUtil.indexNames) {
            xssfSheet.createRow(4);
            for (int i = 0; i < ZhuDiUtil.indexNames.length; i++) {
                xssfSheet.getRow(4).createCell(i + 1).setCellValue(ZhuDiUtil.indexNames[i]);
            }
        }
        //写入主观权重
        if (null != subjectiveData) {
            for (int i = 0; i < subjectiveData.length; i++) {
                xssfSheet.getRow(5).createCell(i + 1).setCellValue(subjectiveData[i].toString());
            }
        }
        //写入客观权重
        if (null != objectiveData) {
            for (int i = 0; i < objectiveData.length; i++) {
                xssfSheet.getRow(6).createCell(i + 1).setCellValue(objectiveData[i].toString());
            }
        }
        //写入综合权重
        if (null != comprehensiveData) {
            for (int i = 0; i < comprehensiveData.length; i++) {
                xssfSheet.getRow(7).createCell(i + 1).setCellValue(comprehensiveData[i].toString());
            }
        }
        //写入案例名
        if (null != ZhuDiUtil.caseNames) {
            for (int i = 0; i < ZhuDiUtil.caseNames.length; i++) {
                xssfSheet.getRow(9).createCell(i + 1).setCellValue(ZhuDiUtil.caseNames[i]);
            }
        }
        //写入TopSis
        if (null != topSisData) {
            xssfSheet.createRow(10);
            for (int i = 0; i < topSisData.length; i++) {
                xssfSheet.getRow(10).createCell(i + 1).setCellValue(topSisData[i].toString());
            }
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(ZhuDiUtil.LOCAL_FILE_PATH + ZhuDiUtil.FILE_NAME));
            xssfWorkbook.write(fos);
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }
}
